package com.hit.aircraftwar.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hit.aircraftwar.R;
import com.hit.aircraftwar.setter.ParameterSet;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    private boolean confirmed = false;
    private EditText et_account;
    private EditText et_password;
    String url = "http://426k04070x.qicp.vip/user/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Objects.requireNonNull(getSupportActionBar()).hide();

        //初始化okhttputils，主要讲cookie持久化，在此还可以进行一些配置
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .build();

        OkHttpUtils.initClient(okHttpClient);

        et_account = findViewById(R.id.et_account);
        et_password = findViewById(R.id.et_password);
        TextView noAccount = findViewById(R.id.noAccount);
        noAccount.setOnClickListener(view -> {
            Intent intent = new Intent(LoginActivity.this, SignActivity.class);
            startActivity(intent);
        });

        Button btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(view -> {
            //进行登入网络请求，应该不需要新建线程
             OkHttpUtils
                    .post()
                    .url(url+"login")
                    .addParams("id", String.valueOf(et_account.getText()))
                    .addParams("password", String.valueOf(et_password.getText()))
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            confirmed =false;
                            e.printStackTrace();
                            Log.i(TAG,"连接失败");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            if (response.equals("true")){
                                confirmed = true;
                                Log.i(TAG,response);
                                Log.i(TAG,"登入成功");

                                ParameterSet.INSTANCE.setName(String.valueOf(et_account.getText()));

                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            }else {
                                confirmed = false;
                                Log.i(TAG,"登入失败");
                                showDialog();
                            }
                        }
                    });
        });

        Button offlineLogin = findViewById(R.id.offlineLogin);
        offlineLogin.setOnClickListener(view -> {
            ParameterSet.INSTANCE.setName("Visitor");
            Log.i(TAG,"登入成功");
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        });
    }

    private void showDialog(){
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("");
        normalDialog.setMessage("用户名或密码错误！");
        normalDialog.setPositiveButton("重新输入",
                (dialog, which) -> {
                    et_account.setText("");
                    et_password.setText("");
                });
        // 显示
        normalDialog.show();
    }
}